//
//  BTEDeviceTypeManager.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/6.
//

import UIKit



public enum deviceMode : String {

//Simulator
case simulator     = "simulator/sandbox",

iPhone7            = "iPhone 7",
iPhone7Plus        = "iPhone 7Plus",
iPhone8            = "iPhone 8",
iPhone8Plus        = "iPhone 8Plus",
iPhoneX            = "iPhone X",
iPhoneXS           = "iPhone XS",
iPhoneXSMax        = "iPhone XSMax",
iPhoneXR           = "iPhone XR",
iPhone11           = "iPhone 11",
iPhone11Pro        = "iPhone 11Pro",
iPhone11ProMax     = "iPhone 11ProMax",
iPhoneSE2          = "iPhone SE2ndgen",
iPhone12Mini       = "iPhone 12Mini",
iPhone12           = "iPhone 12",
iPhone12Pro        = "iPhone 12Pro",
iPhone12ProMax     = "iPhone 12ProMax",
iPhone13Mini       = "iPhone 13Mini",
iPhone13           = "iPhone 13",
iPhone13Pro        = "iPhone 13Pro",
iPhone13ProMax     = "iPhone 13ProMax",
iPhoneSE3          = "iPhone SE3ndgen",
iPhone14           = "iPhone 14",
iPhone14Plus       = "iPhone 14Plus",
iPhone14Pro        = "iPhone 14Pro",
iPhone14ProMax     = "iPhone 14ProMax",
iPhone15           = "iPhone 15",
iPhone15Plus       = "iPhone 15Plus",
iPhone15Pro        = "iPhone 15Pro",
iPhone15ProMax     = "iPhone 15ProMax",
iPhone16           = "iPhone 16" ,
iPhone16Plus       = "iPhone 16Plus",
iPhone16Pro        = "iPhone 16Pro",
iPhone16ProMax     = "iPhone 16ProMax",
iPhone16e          = "iPhone 16e",

     //iPad
          iPad2              = "iPad 2",
          iPad3              = "iPad 3",
          iPad4              = "iPad 4",
          iPadAir            = "iPad Air ",
          iPadAir2           = "iPad Air 2",
          iPadAir3           = "iPad Air 3",
          iPadAir4           = "iPad Air 4",
          iPadAir5           = "iPad Air 5",
          iPad5              = "iPad 5", //iPad 2017
          iPad6              = "iPad 6", //iPad 2018
          iPad7              = "iPad 7", //iPad 2019
          iPad8              = "iPad 8", //iPad 2020
          iPad9              = "iPad 9", //iPad 2021
          iPad10             = "iPad 10", //iPad 2022

          //iPad Mini
          iPadMini           = "iPad Mini",
          iPadMini2          = "iPad Mini 2",
          iPadMini3          = "iPad Mini 3",
          iPadMini4          = "iPad Mini 4",
          iPadMini5          = "iPad Mini 5",
          iPadMini6          = "iPad Mini 6",

          //iPad Pro
          iPadPro9_7         = "iPad Pro 9.7\"",
          iPadPro10_5        = "iPad Pro 10.5\"",
          iPadPro11          = "iPad Pro 11\"",
          iPadPro2_11        = "iPad Pro 11\" 2nd gen",
          iPadPro3_11        = "iPad Pro 11\" 3rd gen",
          iPadPro12_9        = "iPad Pro 12.9\"",
          iPadPro2_12_9      = "iPad Pro 2 12.9\"",
          iPadPro3_12_9      = "iPad Pro 3 12.9\"",
          iPadPro4_12_9      = "iPad Pro 4 12.9\"",
          iPadPro5_12_9      = "iPad Pro 5 12.9\"",
              
     
     
unrecognized       = ""
}

// #-#-#-#-#-#-#-#-#-#-#-#-#
// MARK: UIDevice extensions
// #-#-#-#-#-#-#-#-#-#-#-#-#

public extension UIDevice {
    
    public var tegge: String {
        return "fewfewfew"
    }
    
    public var types: deviceMode {
        var systemInfo = utsname()
        uname(&systemInfo)
        let modelCode = withUnsafePointer(to: &systemInfo.machine) {
            $0.withMemoryRebound(to: CChar.self, capacity: 1) {
                ptr in String.init(validatingUTF8: ptr)
            }
        }
    
        let modelMap : [String: deviceMode] = [
            //Simulator
            "i386"      : .simulator,
            "x86_64"    : .simulator,

            "iPhone9,1" : .iPhone7,
            "iPhone9,3" : .iPhone7,
            "iPhone9,2" : .iPhone7Plus,
            "iPhone9,4" : .iPhone7Plus,
            "iPhone10,1" : .iPhone8,
            "iPhone10,4" : .iPhone8,
            "iPhone10,2" : .iPhone8Plus,
            "iPhone10,5" : .iPhone8Plus,
            "iPhone10,3" : .iPhoneX,
            "iPhone10,6" : .iPhoneX,
            "iPhone11,2" : .iPhoneXS,
            "iPhone11,4" : .iPhoneXSMax,
            "iPhone11,6" : .iPhoneXSMax,
            "iPhone11,8" : .iPhoneXR,
            "iPhone12,1" : .iPhone11,
            "iPhone12,3" : .iPhone11Pro,
            "iPhone12,5" : .iPhone11ProMax,
            "iPhone12,8" : .iPhoneSE2,
            "iPhone13,1" : .iPhone12Mini,
            "iPhone13,2" : .iPhone12,
            "iPhone13,3" : .iPhone12Pro,
            "iPhone13,4" : .iPhone12ProMax,
            "iPhone14,4" : .iPhone13Mini,
            "iPhone14,5" : .iPhone13,
            "iPhone14,2" : .iPhone13Pro,
            "iPhone14,3" : .iPhone13ProMax,
            "iPhone14,6" : .iPhoneSE3,
            "iPhone14,7" : .iPhone14,
            "iPhone14,8" : .iPhone14Plus,
            "iPhone15,2" : .iPhone14Pro,
            "iPhone15,3" : .iPhone14ProMax,
            "iPhone15,4" : .iPhone15,
            "iPhone15,5" : .iPhone15Plus,
            "iPhone16,1" : .iPhone15Pro,
            "iPhone16,2" : .iPhone15ProMax,
            "iPhone17,3" : .iPhone16,
            "iPhone17,4" : .iPhone16Plus,
            "iPhone17,1" : .iPhone16Pro,
            "iPhone17,2" : .iPhone16ProMax,
            "iPhone17,5" : .iPhone16e,
            
            //iPad
            "iPad2,1"   : .iPad2,
            "iPad2,2"   : .iPad2,
            "iPad2,3"   : .iPad2,
            "iPad2,4"   : .iPad2,
            "iPad3,1"   : .iPad3,
            "iPad3,2"   : .iPad3,
            "iPad3,3"   : .iPad3,
            "iPad3,4"   : .iPad4,
            "iPad3,5"   : .iPad4,
            "iPad3,6"   : .iPad4,
            "iPad6,11"  : .iPad5, //iPad 2017
            "iPad6,12"  : .iPad5,
            "iPad7,5"   : .iPad6, //iPad 2018
            "iPad7,6"   : .iPad6,
            "iPad7,11"  : .iPad7, //iPad 2019
            "iPad7,12"  : .iPad7,
            "iPad11,6"  : .iPad8, //iPad 2020
            "iPad11,7"  : .iPad8,
            "iPad12,1"  : .iPad9, //iPad 2021
            "iPad12,2"  : .iPad9,
            "iPad13,18" : .iPad10,
            "iPad13,19" : .iPad10,
    
            //iPad Mini
            "iPad2,5"   : .iPadMini,
            "iPad2,6"   : .iPadMini,
            "iPad2,7"   : .iPadMini,
            "iPad4,4"   : .iPadMini2,
            "iPad4,5"   : .iPadMini2,
            "iPad4,6"   : .iPadMini2,
            "iPad4,7"   : .iPadMini3,
            "iPad4,8"   : .iPadMini3,
            "iPad4,9"   : .iPadMini3,
            "iPad5,1"   : .iPadMini4,
            "iPad5,2"   : .iPadMini4,
            "iPad11,1"  : .iPadMini5,
            "iPad11,2"  : .iPadMini5,
            "iPad14,1"  : .iPadMini6,
            "iPad14,2"  : .iPadMini6,
    
            //iPad Pro
            "iPad6,3"   : .iPadPro9_7,
            "iPad6,4"   : .iPadPro9_7,
            "iPad7,3"   : .iPadPro10_5,
            "iPad7,4"   : .iPadPro10_5,
            "iPad6,7"   : .iPadPro12_9,
            "iPad6,8"   : .iPadPro12_9,
            "iPad7,1"   : .iPadPro2_12_9,
            "iPad7,2"   : .iPadPro2_12_9,
            "iPad8,1"   : .iPadPro11,
            "iPad8,2"   : .iPadPro11,
            "iPad8,3"   : .iPadPro11,
            "iPad8,4"   : .iPadPro11,
            "iPad8,9"   : .iPadPro2_11,
            "iPad8,10"  : .iPadPro2_11,
            "iPad13,4"  : .iPadPro3_11,
            "iPad13,5"  : .iPadPro3_11,
            "iPad13,6"  : .iPadPro3_11,
            "iPad13,7"  : .iPadPro3_11,
            "iPad8,5"   : .iPadPro3_12_9,
            "iPad8,6"   : .iPadPro3_12_9,
            "iPad8,7"   : .iPadPro3_12_9,
            "iPad8,8"   : .iPadPro3_12_9,
            "iPad8,11"  : .iPadPro4_12_9,
            "iPad8,12"  : .iPadPro4_12_9,
            "iPad13,8"  : .iPadPro5_12_9,
            "iPad13,9"  : .iPadPro5_12_9,
            "iPad13,10" : .iPadPro5_12_9,
            "iPad13,11" : .iPadPro5_12_9,
    
            //iPad Air
            "iPad4,1"   : .iPadAir,
            "iPad4,2"   : .iPadAir,
            "iPad4,3"   : .iPadAir,
            "iPad5,3"   : .iPadAir2,
            "iPad5,4"   : .iPadAir2,
            "iPad11,3"  : .iPadAir3,
            "iPad11,4"  : .iPadAir3,
            "iPad13,1"  : .iPadAir4,
            "iPad13,2"  : .iPadAir4,
            "iPad13,16" : .iPadAir5,
            "iPad13,17" : .iPadAir5,

        ]
    
        guard let mcode = modelCode, let model = modelMap[mcode] else { return deviceMode.unrecognized }
        if model == .simulator {
           if let simModelCode = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
              if let simModel = modelMap[simModelCode] {
                return simModel
              }
           }
        }
        return model
    }
}
